<?php $this->placeholder('container-class')->set('post_ad detail job')?>
<?php $this->placeholder('grid-size')->set(12);?>
<?php $this->placeholder('container-class')->set('post_ad select_category')?>
<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plugins.js'))?>
<?php $this->JQuery()->javascriptCaptureStart()?>
var level1 = level2 = "";

function reinit(ele) {
    level1 = level2 = "";
    $('#category2').addClass('hide').removeClass('show');
    $('#category2 ul').addClass('hide').removeClass('show');

    $('#category1 li').removeClass('selected');
    $('#category2 li').removeClass('selected');
    $('#bouya').addClass('hide');
    $('#bouya').hide();
    $('#confirm_category').attr('disabled', 'true');

}

function initEvent() {
$('.category1').hover(function() {
        if($(this).hasClass("disabled")) {
            return'';
        }
        reinit();
        var category1 = $(this).attr('val');
        $('#'+category1).addClass('show').removeClass('hide');
        $('#category2').addClass('show').removeClass('hide');
        $(this).addClass('selected');
        level1 = $(this).html();
        $('#error_category').hide();

        if ($('#category_continue_div').css('display') != 'none') {
            $('#category_continue_div').hide();
            $('#category_continue_btn').show();
        }

     });

     $('.category2').click(function() {
     	if($(this).hasClass('disabled')){
			return false;
		}
        var category2 = $(this).attr('val');
        level2 = $(this).html();

        if ($('#'+category2).length > 0) {
            $('#'+category2).addClass('show').removeClass('hide');
            $('#confirm_category').attr('disabled', 'true');
        } else {
            $('.cat_selected').removeClass('cat_selected');
            $(this).addClass('cat_selected');
            $('#confirm_category').removeAttr('disabled');
            $('#bouya').removeClass('hide');
            $('#bouya').show();

            var str = level1 + " > "+ level2;
        }

        if ($('#category_continue_div').css('display') != 'none') {
            $('#category_continue_div').hide();
            $('#category_continue_btn').show();
        }

        $(this).addClass('selected');
     });
}

$('document').ready(function(){
	$.each($('.init_text'), function(){
		$(this).data('initText', $(this).val());
	});

	$('.alert').hide();
    if ($('#selected_category_list li').length == 0){
        $('#categories_list').addClass('hide');        
    }
    else {
        $('#categories_list').removeClass('hide');
        $('#category_continue_btn').removeClass('hide');
    }

     initEvent();
     function removeLink(ele) {
        var ele = $(ele);
        $("#hidden_" + ele.attr("rel")).remove();
        $("#li_" + ele.attr("rel")).remove();
        //if we remove the category let's check again and re enable if we have to
        var l = $('#selected_category_list').find('.to_disable');
        if(l.length == 0) {
            $('#category1 li').removeClass('disabled');
            $('#category2 li').removeClass('disabled');
        }
        //Count number of categories
        if ($('#selected_category_list li').length == 0){
            $('#categories_list').addClass('hide');
        }
        $('#category_continue_div').hide();
        $('#text_not_vertical_1').removeClass('hide');
        $('#text_not_vertical_2').removeClass('hide');
        reinit();
        initEvent();
     }
     $(".remove_link").click(function() {
        //Count number of categories
        if ($('#selected_category_list li').length == 0){
            $('#categories_list').addClass('hide');
        }
        $('#category_continue_div').hide();
        reinit();
        removeLink($(this));
     });

     

     $('#category2').click(function () {
        var cat_selected = $($('.cat_selected')[0]);

        var selected = cat_selected.attr('val');
        var a = $(document.createElement("a"))
            .addClass("remove_link")
            .html("<?php echo $this->t('Remove')?>")
            .click(function() {
                removeLink($(this));
            })
            .attr("rel",selected);
        if (cat_selected.hasClass('to_disable')) {
            //This is a vertical
            a.addClass('to_disable');
            $('#category_continue_div').show();
            $('#category_continue_div').removeClass('hide');
            
            $('#text_not_vertical_1').addClass('hide');
            $('#text_not_vertical_2').addClass('hide');
            $('.alert').hide();
        }
        else {
            $('#category_continue_div').show();
            $('#category_continue_div').removeClass('hide');
            $('.alert').show();
        }

        $('#categories_list').removeClass('hide');

        $(document.createElement("li"))
            .attr("id", "li_"+selected)
            .html(level1 + " > "+ level2)
            .appendTo($('.selected_category_list'))
            .append('&nbsp;')
            .append(a);

        $(document.createElement("input"))
            .attr({ type: "hidden", name:"category[]", value:selected, "id":"hidden_"+selected})
            .appendTo($('.selected_category_list'));
		
		cat_selected.addClass('disabled');
		
        if (cat_selected.hasClass('to_disable')) {
            $('#category1 li').addClass('disabled');
            $('#category2 li').addClass('disabled');
        }
        else {
            $('.to_disable').addClass('disabled');
        }
        $('#bouya').hide();
        $('#bouya').addClass('hide');

        $('#category_continue_btn').hide();
        $('#category_continue_btn').addClass('hide');
     });

     $('#select_other_category').click(function () {        
        $('#category_continue_btn').show();
        $('#category_continue_div').hide();
        $('#category_continue_div').addClass('hide');
        reinit();
     });
});


function event_focus_in_out(){	
	$j('.init_text').focusin(function(){
		if($j(this).val() == $j(this).data('initText')){
			$j(this).val('');
		}
		$j(this).removeClass('listing-empty');
    })
    $j('.init_text').focusout(function(){
    	if($j(this).val() == ''){
    		$j(this).val($j(this).data('initText'));
    	}
    
    	if($j(this).val() == $j(this).data('initText'))
    	{
    		$j(this).addClass('listing-empty');
    	}
    })
}

function event_change_local_url(){
	$j('#changetolocal').click(function(){
		$j(this).parent().find('span').text('本地上传图片');
		$j(this).hide();
		$j('.al-fedv').hide();
		$j('#imguplocal').show();
		$j('#changetourl').show();
		$j('.al-prepost').show();
		$j('#al-picselect').hide();
		$j('#show-grab-rote').hide();
		$j('#addmode').val('addbylocal');
	})
	$j('#changetourl').click(function(){
		$j(this).parent().find('span').text('粘贴网址抓取');
		$j(this).hide();
		$j('#changetolocal').show();
		$j('#addmode').val('addbyurl');
		
		if($j('#al-picselect .al-piclist .vm').length>0){   //if images exist
			$('.al-prepost').hide();
			$('#al-picselect').show();
			$('#show-grab-rote').show();
		}else{
			$j('.al-fedv').show();
			$j('#imguplocal').hide();
			
			$j('.al-piclist').html('');
			$j('#show-grab-rote').html('');
			$j('#tl_en').val('').focusout();
			$j('#pr').val('').focusout();
			$j('#al-fipt').val('http://');
			
			$('#al-fchk').text('拷贝完整的网址，我们将帮你保存网页的图片和地址。').css('color','#bbbbbb');
		}	
	})

}

function event_select_tags(){
	$j('#al-tagssel a').click(function(){
		if($(this).hasClass('added'))
		{
			$j('#al-tagsipt')[0].focus();
			return false;
		}else{
			$j(this).addClass('added');
			$j('#al-tagsipt').focus();
			$j('#al-tagsipt').val($j('#al-tagsipt').val()+$j(this).text()+' ');
		}
	})
	
	$j('#al-tagsipt').keyup(function(){
		var tagArr = $j(this).val().split(' ');
		$j('#al-tagssel a').each(function(){
			if(jQuery.inArray($j(this).text(), tagArr) == -1){
				$j(this).removeClass('added');
			}else{
				$j(this).addClass('added');
			}
		})
	})

}


function event_click_reupload(){
	$j('#reupload').click(function(){
		$('.al-prepost').show();
		$('#al-picselect').hide();
		$('#show-grab-rote').html('').hide();
		//$j('.remove_photo').click();
		$j('.al-piclist').html('');
		$j('#al-fipt').val('http://');
		$j('#changetourl').click();
		$j('.errors').remove();
		$j('#al-albumsel a').text('点击选择');
		$j('#al-albumsel input').val('');
		$j('#al-tagssel a').removeClass('added');
		
		$.each($('.init_text'), function(){
			$(this).val($(this).data('initText'));
		});
		window.frames['imguplocal'].document.getElementById('remove_photo_0').click();
	});

}


function event_select_category(){
	$j('#top_category li').click(function(){
		$j('#top_category li').removeClass('c_selected');
		$j(this).addClass('c_selected');
		$j('#al-tagssel a').hide();
		$j('#al-tagssel a[class="tag_'+$j(this).attr('id')+'"]').show();
		$j('#second_category_div ul li').hide();
		$j('#second_category_div ul li[val="'+$j('#top_category .c_selected').attr('id')+'"]').show();
	});
	$j('#second_category_div ul li').click(function(){
		$j(this).parent().find('li').removeClass('added');
		$j(this).addClass('added');
		$j('#al-albumsel input[name=category_top]').val($j(this).attr('val'));
    	$j('#al-albumsel input[name=category_specific]').val($j(this).attr('id'));			
	});
	$j('#top_category li:first').click();
}

$j(document).ready(function() {
	event_focus_in_out();
	event_change_local_url();
	event_select_tags();
	event_click_reupload();
	event_select_category();
})
<?php $this->JQuery()->javascriptCaptureEnd() ?>
<?php $errors = $this->form->getErrors(); ?>
<div id="al-mblog-box" style="position: fixed; z-index: 9999; padding: 0px; margin: -291px 0px 0px -480px; width: 960px; top: 50%; left: 50%; text-align: left; color: rgb(0, 0, 0); border: medium none; background: none repeat scroll 0% 0% transparent; height: 596px;" class="blockUI blockMsg blockPage">
	<div id="al-mblog" class="al-fetch detail">
		<div class="al-head">
			<h1>
				<?php if(count($this->form->img) > 0):?>
				<span>本地上传图片</span>
				<a href="javascript:;" class="al-swbtn" id="changetolocal" style="display: none;">切换至[本地上传图片]</a>
				<a href="javascript:;" class="al-swbtn" id="changetourl">切换至[黏贴网址抓取]</a>
				<?php else:?>
				<span>粘贴网址抓取</span>
				<a href="javascript:;" class="al-swbtn" id="changetolocal">切换至[本地上传图片]</a>
				<a href="javascript:;" class="al-swbtn" id="changetourl" style="display: none;">切换至[黏贴网址抓取]</a>
				<?php endif;?>
				<a href="javascript:;" class="al-swbtn" id="reupload">重新上传</a>
			</h1>
		</div>
        
        <div class="al-prepost">
			<div class="al-covb">
			</div>
			<div class="al-covh">
			</div>
			<div class="clr al-covv">
				<div class="al-fedv" style=
								<?php if(count($this->form->img) > 0):?>
								"display:none;"
								<?php else:?>
								"display:block;"
								<?php endif;?>
								>
					<div id="al-fchk" class="al-fchk gray">
						拷贝完整的网址，我们将帮你保存网页的图片和地址。
					</div>
					<input type="text" id="al-fipt" name="url" class="ipt" autocomplete="off" value="http://">
					<div class="tc">
						<a id="al-fchab" class="abtn abtn-s dib" href="javascript:;" target="_self">
							<button id="grab_content" type="button">
								<u>
									确定
								</u>
							</button>
							<?php $this->JQuery()->javascriptCaptureStart() ?>
	
	                        	var $ = $j;
								
	                            $('#grab_content').click(function(){
									var url = $('#al-fipt[name="url"]').val();
									$.ajax({
										type: 'GET',
										url: '<?php echo $this->geturl('grabcontent', 'listing', null, 'default'); ?>',
										data: {
											url: url
										},
										beforeSend: function(){
											$('.al-prepost').hide();
											$('#al-picselect').show();
											$('#show-grab-rote').show();
											$('#show-landing').show();
										},
										success: function(content){
											$('#show-landing').hide();
											if(content.tb){
												if(content.tb.item_imgs.item_img){
													$('.al-prepost').hide();
													$('#al-picselect').show();
													$('#show-grab-rote').show();
													$.each(content.tb.item_imgs.item_img, function(key, img){
														var inner = "";
															inner += "<div class='vm ct l'>";
															inner += 	"<div class='vma'>";
															inner += 		"<div class='vmb'> ";	
															inner += 			"<img class='tbimg' width='198' height='198' src='"+img.url+"' style='visibility: visible;'>"
															inner += 		"</div>";
															inner += 	"</div>";
															inner += "</div>";
														$('.al-piclist').append(inner);
													})
													var innerTitle = "";
														innerTitle += "<div style='color:#666666;font-size:12px;height:15px;overflow:hidden;'><span style='color:#BBBBBB;'>来源:</span> "+content.tb.title+"</div>";
														innerTitle += "<div style='color:#666666;font-size:12px;height:15px;overflow:hidden;'><span style='color:#BBBBBB;'>链接:</span> "+content.tb.detail_url+"</div>";
													$('#show-grab-rote').append(innerTitle);
													
													$('#tl_en').val(content.tb.title).removeClass('listing-empty');
													$('#pr').val(content.tb.price).removeClass('listing-empty');
												}
											}else if(content.otherSite){
												if(content.otherSite.img){
													$('.al-prepost').hide();
													$('#al-picselect').show();
													$('#show-grab-rote').show();
													$.each(content.otherSite.img, function(key, img){
														if(img.ELEMENT_ENTIRE){
															var inner = "";
																inner += "<div class='vm ct l'>";
																inner += 	"<div class='vma'>";
																inner += 		"<div class='vmb'> ";	
																inner += 			img.ELEMENT_ENTIRE;
																inner += 		"</div>";
																inner += 	"</div>";
																inner += "</div>";
															$('.al-piclist').append(inner);
														}
													})
													var innerTitle = "";
														
													
													if(content.otherSite.title){
														innerTitle += "<div style='color:#666666;font-size:12px;height:15px;overflow:hidden;'><span style='color:#BBBBBB;'>来源:</span> "+content.otherSite.title[0].ELEMENT_INNER+"</div>";
														$('#tl_en').val(content.otherSite.title[0].ELEMENT_INNER).removeClass('listing-empty');
													}else if(content.otherSite.h1){
														innerTitle += "<div style='color:#666666;font-size:12px;height:15px;overflow:hidden;'><span style='color:#BBBBBB;'>来源:</span> "+content.otherSite.h1[0].ELEMENT_INNER+"</div>";
														$('#tl_en').val(content.otherSite.h1[0].ELEMENT_INNER).removeClass('listing-empty');
													}else{
														innerTitle += "<div style='color:#666666;font-size:12px;height:15px;overflow:hidden;'><span style='color:#BBBBBB;'>来源:</span> "+content.otherSite.url+"</div>";
													}
													innerTitle += "<div style='color:#666666;font-size:12px;height:15px;overflow:hidden;'><span style='color:#BBBBBB;'>链接:</span> "+content.otherSite.url+"</div>";
													$('#show-grab-rote').append(innerTitle);
												}
											}
											
											$('.al-piclist').css({
												'width': 0,
												'height': 202,
												overflow: 'hidden'
											});
											$('.vmb img').load(function(){
												var newImg = new Image();
												newImg.src = $(this)[0].src;
												if(newImg.width < 10 || newImg.height < 10 || newImg.onerror){		//filter tiny images
													$(this).parents('.vm').remove();
													return true;
												}
												$(this).picPortionResize({
													maxWidth: 500,
    												maxHeight: 198
												});
												var piclistWidth = $('.al-piclist').width();
												piclistWidth += $(this).parents('.vm').outerWidth() + 10;
												$('.al-piclist').css('width', piclistWidth);
											});
											
											$.each($('.al-piclist .vm'), function(){
												if($(this).width() < 10){
													$(this).remove();
												}
											});
											
											if($('.al-piclist .vm').size() <= 0){
												$('.al-prepost').show();
												$('#al-picselect').hide();
												$('#show-grab-rote').hide();
												$('#show-landing').hide();
												$('#al-fchk').text('页面不存在，或页面上没有找到合适的图片').css('color','red');
											}
										},
										error: function(){
											$('.al-prepost').show();
											$('#al-picselect').hide();
											$('#show-grab-rote').hide();
											$('#show-landing').hide();
											$('#al-fchk').text('页面不存在，或页面上没有找到合适的图片').css('color','red');
										}
										//dataType: 'json'
									});
										
		
									return false;
								});
								
							<?php $this->JQuery()->javascriptCaptureEnd() ?>
						</a>
					</div>
				</div>
				
				<?php //echo $this->Partial('/index/photo-uploader-local.phtml', 'listing', array('listing' => $this->listing, 'adid' => $this->adid));?>
				<iframe id="imguplocal" name="imguplocal" src="<?php echo $this->geturl('upimglocal', 'listing', null, 'default'); ?>" style="display: none; border: none; width: 100%; height: 100%; overflow: hidden;"></iframe>
			</div>
			<div id="al-loading"></div>
		</div>
		
        <form action="<?php echo $this->escape($this->form->getAction()) ?>" method="<?php echo $this->escape($this->form->getMethod())?>" id="listing_form"  enctype="multipart/form-data">
            <div class="al-bepost clr photo_uploader" id="al-bepost">
                <div class="al-part-lf">
                    <div id="al-pics">
                    	<div style="width:100%;height:40px;border:0px solid red;display:none;" id="show-grab-rote">
                    		
                    	</div>
                    	<div id="al-picselect" class="al-picselect" style="overflow-x:scroll;display:none;">
                    		<div class="al-piclist clr">
                    		</div>
                    		<div id="show-landing" style="display:none;">
                    			正在抓取，请等待<img src="/images/loading2.gif">
                    		</div>
                    	</div>
                    </div>
                    <div>
						<?php echo $this->form->tl_en; ?>
                    	<span class="required">*</span>
						<?php if(!empty($errors['tl_en'])):?>
			          		<ul class="errors">
			          			<?php foreach($this->form->tl_en->getMessages() as $errmsg): ?>
							  	<li><?php echo $errmsg?></li>
							  	<?php endforeach;?>
							</ul>
			          	<?php endif;?> 
						<?php echo $this->form->pr; ?>
						<?php if(!empty($errors['pr'])):?>
			          		<ul class="errors">
			          			<?php foreach($this->form->pr->getMessages() as $errmsg): ?>
							  	<li><?php echo $errmsg?></li>
							  	<?php endforeach;?>
							</ul>
			          	<?php endif;?> 
                    </div>
                    <div class="al-cxa">
						<?php echo $this->form->cs_en; ?>
                        <label id="al-txa-lb" for="al-txa" style="display: block;">
<!--                            <span class="required">*</span>-->
	                        <?php if(!empty($errors['cs_en'])):?>
				          		<ul class="errors">
				          			<?php foreach($this->form->cs_en->getMessages() as $errmsg): ?>
								  	<li><?php echo $errmsg?></li>
								  	<?php endforeach;?>
								</ul>
				          	<?php endif;?> 
                        </label>
                    </div>
                    <?php echo $this->form->weibocheckbox;?><label for='weibocheckbox' style="width:97%;float:right;">通过微博分享</label>
                    <div class="u-chk al-subbtn clr" style='margin-top:3px;'>
                        <a target="_self" href="#" class="abtn abtn-s l">
                            <button type="submit" id="al-abtnpost">
                                <u>
                                    	发布
                                </u>
                            </button>
                        </a>
                        <input type='hidden' id='addmode' name='addmode'>
                        <div id="al-poststat">
                        </div>
                    </div>
                </div>
                <?php 
					$level1 = Model_Category::getLevel1(array('post_an_ad' => 1, 'isnew'=>true, 'status' => Model_Category::STATUS_ACTIVE))->sort(array('pos' => 1));
					$level2 = Model_Category::getLevel2(array('post_an_ad' => 1, 'isnew'=>true, 'status' => Model_Category::STATUS_ACTIVE))->sort(array('path.0' => 1, 'pos' => 1));
				?>
                <div class="al-part-rg">
                    <div class="al-album" style="z-index: 9999;">
                    	<h6>
                    		<span class="required">*</span>
                    		选择类目
                    		<?php if(!empty($errors['category_specific'])):?>
				          		<ul class="errors">
				          			<?php foreach($this->form->category_specific->getMessages() as $errmsg): ?>
								  	<li><?php echo $errmsg?></li>
								  	<?php endforeach;?>
								</ul>
				          	<?php endif;?>
                    	</h6>
                    	<div id="al-albumsel" class="al-albumsel">
							<div style="width:228px;height:200px;">
								<div id="top_category_div">
									<ul id="top_category">
										<?php foreach ($level1 as $top_ct):?>
				    					<li class="top_ct" id="<?php echo $top_ct->_id;?>">
				    						<?php echo $top_ct->name;?>
				    					</li>
				    					<?php endforeach;?>
									</ul>
								</div>
								<div id="second_category_div">
									<ul>
					    				<?php foreach ($level2 as $second_ct):?>
					    				<li val="<?php echo $second_ct->path[0];?>" id="<?php echo $second_ct->_id?>"><?php echo $second_ct->name;?></li>
					    				<?php endforeach;?>
					    			</ul>
								</div>
							</div>
                    		<?php echo $this->form->category_top;?>
                    		<?php echo $this->form->category_specific;?>
                    	</div>
                    	<h6 style="float: none;margin-top:130px;">常用标签</h6>
                    	<div class="usetag clr" id="al-tagssel" style="width:245px;">
                    		<?php $i=0;?>
							<?php foreach ($level1 as $key=>$val):?>
								<?php if ($i==0):?>
									<?php foreach ($val->tag as $t):?>
										<a href="#" class="tag_<?php echo $val->_id;?>" style="display: block;"><?php echo trim($t);?></a>
									<?php endforeach;?>
								<?php else :?>
									<?php foreach ($val->tag as $t):?>
										<a href="#" class="tag_<?php echo $val->_id;?>"><?php echo trim($t);?></a>
									<?php endforeach;?>
								<?php endif;?>
								<?php $i++;?>
							<?php endforeach;?>
						</div>
						<h6 style="float: none;">添加标签</h6>
						<?php echo $this->form->tags;?>
						<?php if(!empty($errors['tags'])):?>
			          		<ul class="errors">
			          			<?php foreach($this->form->tags->getMessages() as $errmsg): ?>
							  	<li><?php echo $errmsg?></li>
							  	<?php endforeach;?>
							</ul>
			          	<?php endif;?>
                    </div>
				    
				        
			        <?php if(!empty($this->infos)):?>
		          		<ul class="errors">
		          			<?php foreach($this->infos as $info): ?>
						  	<li><?php echo $info?></li>
						  	<?php endforeach;?>
						</ul>
		          	<?php endif;?>
                </div>
            </div>
        </form>
	</div>
</div>